package com.frinder.official.akka.routing;

import com.frinder.official.spring.MyBaseActor;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static akka.routing.ConsistentHashingRouter.ConsistentHashable;

/**
 * Created by frinder on 2017/9/29.
 */
@Component
@Scope("prototype")
public class PayloadActor extends MyBaseActor {

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(Payload.class, p -> {
                    log.info("*******************************");
                    log.info("{} {} {}", getSender(), getSelf(), p);
                    log.info("*******************************");
                })
                .build();
    }

    @Data
    @ToString
    public static class Payload {
        protected String key;
        protected String value;

        public Payload(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    @Data
    public static class Payload2 extends Payload implements ConsistentHashable {

        public Payload2(String key, String value) {
            super(key, value);
        }

        @Override
        public Object consistentHashKey() {
            return key;
        }

        @Override
        public String toString() {
            return "Payload2{" +
                    "key='" + key + '\'' +
                    ", value='" + value + '\'' +
                    "} " + super.toString();
        }
    }
}
